
//window.indexedDB = window.indexedDB || window.mozIndexedDB || window.webkitIndexedDB || window.msIndexedDB;

if (!window.indexedDB) {
    alert('你的浏览器不支持indexedDB');
} 

let version = 1;
let dbName = 'rightli';
let db;
let request;
function load () {
    request = window.indexedDB.open(dbName, version);
    request.onerror = function (event) {
        console.log('发生错误 =====');
        console.log(event);
    };

    request.onupgradeneeded = function (event) {
        console.log('upgrade ==='); 
        db = event.target.result;
        var objectStore = db.createObjectStore("students", { keyPath : "rollNo" });
    };

    request.onsuccess = function (event) {
        console.log('成功 ==== ');
        db = event.target.result;
    };
}


function add () {
    var transaction = db.transaction(["students"],"readwrite");
    transaction.oncomplete = function(event) {
        console.log("Success");
    };

    transaction.onerror = function(event) {
        console.log("Error");
    };  
    var objectStore = transaction.objectStore("students");

    objectStore.add({rollNo: '377', name: 'wenzi.hua'});
}

function get () {
    var request = db.transaction(["students"],"readwrite").objectStore("students").get('377');
    request.onsuccess = function(event){
        console.log("Name : "+request.result.name);    
    };
}

// function delete () {
//     db.transaction(["students"],"readwrite").objectStore("students").delete('377');
// }

function update () {
    var transaction = db.transaction(["students"],"readwrite");
    var objectStore = transaction.objectStore("students");
    var request = objectStore.get(rollNo);
    request.onsuccess = function(event){
        console.log("Updating : "+request.result.name + " to " + name);
        request.result.name = name;
        objectStore.put(request.result);
    };
}

window.onload = load;

$('#app').on('click', () => {
    add();
});
$('#show').on('click', () => {
    get();
});
